<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------


/**
 * 文章分类ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

quanli_require('module/base/content/contentArticle.class.php');

class ajax_articleCategory {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询文章分类表信息
                    $this -> searchArticleCategoryList();
                    break;

                case 2://查询文章分类详情
                    $this->searchArticleCategoryDetail();
                    break;

                case 100:
                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('del', 'edit','change'))) {

                        returnJsonData('参数错误');

                    }

                    if ($_REQUEST['act'] == 'edit') {

                        $this->saveArticleCategory();//添加 修改 分类文章

                    } else if ($_REQUEST['act'] == 'del') {

                        $this->deleteArticleCategory();//删除文章分类信息

                    } else if ($_REQUEST['act'] == 'change') {

                        $this->changeArticleCategoryUseflg(); // 启用/禁用

                    }

                    break;

                default:
                    returnJsonData('参数错误');
                    break;


            }

        } else {
            returnJsonData('参数错误');
        }

    }

    /**
     * 查询文章分类信息
     */
    private function searchArticleCategoryList() {

        global $G_PGLOB;

        //实例文章类
        $contentArticle = new contentArticle();

        //查询文章分类
        $category_array = $contentArticle->searchArticleCategory( $_REQUEST );


        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($category_array) {

            $edit_permission = $G_PGLOB->has_permission(1565);

            foreach($category_array as &$cate) {

                if($edit_permission){

                    if( $cate['useFlg'] == 1 ) {
                        $useStatus =  '<a href="javascript: void(0);" onclick="article.change.check(' . $cate['category_id'] . ', ' . $cate['useFlg'] .');">启用</a>';
                    } else{
                        $useStatus =  '<a href="javascript: void(0);" onclick="article.change.check(' . $cate['category_id'] . ', ' . $cate['useFlg'] .');">禁用</a>';
                    }

                    $cate_id = &$cate['category_id'];
                    if ($cate['category_type'] == 1)
                        $category_type_name = '帮助文章';
                    else if ($cate['category_type'] == 2)
                        $category_type_name = '公告文章';
                    else if ($cate['category_type'] == 3)
                        $category_type_name = '新闻文章';
                    else
                        $category_type_name = '';
                    $result_array['rows'][] = array(
                        'id' => $cate_id,
                        'cell' => array(
                            '<a href="javascript: void(0);" onclick="article.edit.open(' . $cate_id . ');">修改</a>&nbsp;',
                            $cate['category_id'],
                            '&nbsp;&nbsp;&nbsp;&nbsp;' . $cate['category_title'],
                            $cate['category_index'],
                            $category_type_name,
                            $cate['description'],
                            $useStatus
                        )
                    );


                }

            }

        }

        echo json_encode($result_array);

    }

    /**
     * 保存或编辑文章分类信息
     */
    private function saveArticleCategory() {

        global $G_PGLOB;

        //实例文章类
        $contentArticle = new contentArticle();

        $_REQUEST['creator'] = $G_PGLOB->currentUserID;

        $result_array = $contentArticle->addArticleCategory( $_REQUEST );

        returnJsonData($result_array['message'], $result_array['code']);

    }

    /**
     * 查询文章分类方式
     */
    private function searchArticleCategoryDetail() {

        if (!isset($_REQUEST['category_id']) || !quanli_is_int($_REQUEST['category_id'])) {
            returnJsonData('参数错误');
        }

        //实例文章类
        $contentArticle = new contentArticle();

        //查询文章分类方式
        $arg_array = array(
            'category_id' => $_REQUEST['category_id']
        );
        $article_category_array = $contentArticle->searchArticleCategory( $arg_array );

        if ($article_category_array) {
            returnJsonData('ok', 200, $article_category_array[0]);
        } else {
            returnJsonData('文章分类信息没有找到', 404);
        }

    }

    /**
     * 删除文章分类信息
     */
    private function deleteArticleCategory() {

        //实例文章类
        $contentArticle = new contentArticle();

        $result_array = $contentArticle->deleteArticleCategory( $_REQUEST );

        returnJsonData($result_array['message'], $result_array['code']);

    }

    /**
     * 文章分类 启用/禁用
     */
    private function changeArticleCategoryUseflg () {

        //实例文章类
        $contentArticle = new contentArticle();

        //文章分类启用/禁用
        $result_array = $contentArticle->changeArticleCategoryUseflg( $_REQUEST );

        returnJsonData( $result_array['message'], $result_array['code'] );

    }


}